/*
 * File Name : shell_controller.cpp
 * Auther : dream.liuguobiao
 * Create Date : 2010-5-14
 * Last Edit : 2010-5-16
 */
#include <iostream>
#include <string>

#include <termios.h>
#include <unistd.h>

#include "shell_controller.h"
#include "command_factory.h"
#include "shell_environment.h"

#include "my_string.h"

shell_controller::shell_controller()
:my_environment( shell_environment::get_shell_environment() )
,my_command_factory(command_factory::get_command_factory())
{
}

void shell_controller::run(){
//  start();
  while(login());
  exec_command();
}

int shell_controller::login(){
  std::string input_string;
  char c;
  users *all_users = my_environment->get_all_users();
  user *login_user = my_environment->get_administrator();
  std::stack<user*> *login_users = my_environment->get_login_users();
  my_string *string_tools = my_string::get_my_string();

  std::cout<<std::endl<<"Dream_Host login: ";
  std::getline( std::cin , input_string );
  if( (std::string)"root" != input_string ) {
    login_user = all_users->find_user_by_name( &input_string );
  }
  std::cout<<"Password: ";
  input_string = "";
  while(1){
    c = string_tools->mygetch();
    if(c == '\n')break;
    input_string += c;
  }
  if( NULL == login_user ||  input_string != *(login_user->get_password() ) ){
    std::cout<<std::endl<<"Login incorrect"<<std::endl;
    return 1;
  }
  login_users->push(login_user);
  std::cout<<std::endl<<std::endl;
  std::cout<<"Linux 2.6.33-Dream-1.3.\nLast login: Sun May 16 08:39:39 +0800 2010 on tty1.\nNo mail.\nWe have not inherited the earth from our parents, we've borrowed it from\nour children.\n";
  return 0;
}

bool shell_controller::exec_command()
{
  std::stack<user*> *login_users = my_environment->get_login_users();
  file_system *my_file_system = my_environment->get_file_system();
  std::string input_string;

  do{
    std::string *user_name = login_users->top()->get_name();
    std::string *path = my_file_system->get_current_directory()->get_path();
    std::cout<<*user_name<<"@Dream_Host:"<<*path<<"$ ";
    std::getline( std::cin , input_string );
    command *current_command = my_command_factory->get_command( &input_string );
    if( NULL == current_command){
      std::cout<<"-bash: "<<input_string<<": command not found\n";
      continue;
    }
    int command_return = current_command->run_command();
    if( 0 ==  command_return) {
      //  do notthing  for command run successfully
    }else if( 1 == command_return ){
      while(login()); 
      delete current_command;
      continue;
    }else if( 9 == command_return ){ //halt system
      break;
    }else{
      std::cout<<"\nSystem Catch A Error Which current_command->run_command() return an undefine type\n In shell_controller.cpp function: shell_controller::exec_command() \n";
    }
    delete current_command;
  }while(1);

  return 0;
}

void shell_controller::start()
{
    std::cout<<std::endl;
    sleep(1);
    std::cout<<"Bringing up interface eth0..............................................[OK]\n";
    sleep(1);
    std::cout<<"Determining IP information for eth0... done.............................[OK]\n";
    sleep(1);
    std::cout<<"Starting auditd:........................................................[OK]\n";
    sleep(1);
    std::cout<<"Starting kernel logger:.................................................[OK]\n";
    sleep(1);
    std::cout<<"Starting system message bus:............................................[OK]\n";
    sleep(1);
    std::cout<<"Systerm Now had start , Do login :......................................[OK]\n";
    sleep(2);
}
